package com.lu.aio.server;

import cn.hutool.core.util.CharsetUtil;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.TimeUnit;

/**
 * @program lu-netty
 * @description: 客户端连接处理器
 * @author: zhanglu
 * @create: 2022-06-10 18:18:00
 */
public class AioServerAcceptCompletionHandler implements CompletionHandler<AsynchronousSocketChannel, AioServer> {

    /**
     * 连接成功 - 初始化请求处理器
     * @param channel
     * @param attachment
     */
    @Override
    public void completed(AsynchronousSocketChannel channel, AioServer attachment) {
        try {
            //接收新的客户端
            attachment.serverSocketChannel().accept(attachment, this);
            ByteBuffer buffer = ByteBuffer.allocate(1024);

            channel.read(buffer, buffer, new ReadCompletionHandler(channel));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

    @Override
    public void failed(Throwable exc, AioServer attachment) {
        exc.printStackTrace();
    }

}
